<?php

namespace app\admin\model;

use think\Model;


class Pmsreserveinfo extends Model
{

    

    

    // 表名
    protected $name = 'pms_reserve_info';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'check_in_time_text',
        'check_out_time_text'
    ];
    

    



    public function getCheckInTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_in_time']) ? $data['check_in_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCheckOutTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_out_time']) ? $data['check_out_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCheckInTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCheckOutTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function get_reserve_status_name($order_status)
    {
        $re = '作废预定';
        switch($order_status)
        {
            case 0:
                $re = '新预定';
                break;
            case 1:
                $re = '已转订单';
                break;
        }

        return $re;
    }

    public function create_reserve_no($code)
    {
        $re = '';
        $ymd = date('ymd');
        $m = $this->field('reserve_no')->order('id desc')->find();

        if ($m)
        {
            $reserve_no = $m['reserve_no'];

            $reserve_no = substr($reserve_no, strlen($code), strlen($reserve_no) - strlen($code) - 1);
            $numstr = '001';
            if (substr($reserve_no, 0, strlen($ymd)) == $ymd)
            {
                $num = substr($reserve_no, strlen($ymd), strlen($reserve_no)) + 0 + 1;
                $numstr = '00' . $num;
                $numstr = substr($numstr, strlen($numstr) - 3, 3);
            }

            $re = $ymd . $numstr;
        }
        else
        {
            $re = $ymd . '001';
        }

        return $code . $re . 'B';
    }
}
